package com.ny.demo;

// 定义一个抽象类Pet
abstract class Pet {
    private String name;
    private int age;

    // 构造函数
    public Pet(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // 获取宠物的名字
    public String getName() {
        return name;
    }

    // 获取宠物的年龄
    public int getAge() {
        return age;
    }

    // 抽象方法，用于发出声音
    public abstract String makeSound();
}

// 定义一个Dog类，继承自Pet
class Dog extends Pet {
    public Dog(String name, int age) {
        super(name, age);
    }

    @Override
    public String makeSound() {
        return "Woof!";
    }
}

// 定义一个Cat类，继承自Pet
class Cat extends Pet {
    public Cat(String name, int age) {
        super(name, age);
    }

    @Override
    public String makeSound() {
        return "Meow!";
    }
}



    // 主类
    public class main {
        public static void main(String[] args) {
            Pet dog = new Dog("Buddy", 3);
            Pet cat = new Cat("Whiskers", 2);

            System.out.println(dog.getName() + ", " + dog.getAge() + " years old, says " + dog.makeSound());
            System.out.println(cat.getName() + ", " + cat.getAge() + " years old, says " + cat.makeSound());
        }
    }

